<?php

class AccountController extends Controller
{
	public function actionActivate()
	{
		$this->render('activate');
	}

	public function actionIndex()
	{
		$this->render('register');
	}

	public function actionLogin()
	{
		$model=new User;

		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		// collect user input data
		if(isset($_POST['User']))
		{
			$model->attributes=$_POST['User'];
			// validate user input and redirect to the previous page if valid
			if($model->validate() && $model->login())
				$this->redirect(Yii::app()->user->returnUrl);
		}
		// display the login form
		$this->render('login',array('model'=>$model));
	}

	public function actionLogout()
	{
        Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}

	public function actionLost_password()
	{
        // If lost password action is allowed
        if (Yii::app()->params['lostPassword'] !== '0')
        {
            $model=new User('lost_password');

            // collect user input data
            if(isset($_POST['User']))
            {
                $model->attributes=$_POST['User'];
                // validate user input
                if($model->validate())
                {
                    $user=User::model()->findByAttributes(array('email'=>$model->email));
                    $token=new Token();
                    $token->user_id=$user->id;
                    $token->action='email_recovery';
                    if($token->save())
                    {
                        $this->redirect(Yii::app()->user->loginUrl);
                    }
                }
            }
            // display the login form
            $this->render('lost_password',array('model'=>$model));
        }
        else
            $this->redirect(Yii::app()->homeUrl);
	}

	public function actionRegister()
	{
        // If self registration is allowed
        if (Yii::app()->params['selfRegistrationMode'] !== '0')
        {
            $model=new User('register');
            // If registration form posted, compute data
            if(isset($_POST['User']))
            {
                $model->attributes=$_POST['User'];
                // User is not an admin by default
                $model->is_admin = 0;
                // Password encryption
                $model->salt=substr(md5(uniqid(rand(), true)),0,CRYPT_SALT_LENGTH);
                $model->password=crypt($model->password,$model->salt);
                $model->password_repeat=crypt($model->password_repeat,$model->salt);
                // If data saved sucessfully go to home page, else stay on registration page
                if($model->save())
                    $this->redirect(Yii::app()->homeUrl);
                else
                    $this->render('register',array('model'=>$model));
            }
            // Else display registration form
            else
                $this->render('register',array('model'=>$model));
        }
        else
            $this->redirect(Yii::app()->homeUrl);
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}